//
//  FilterCriteriaVideoInfoViewController.swift
//  月升
//
//  Created by 拉利法 on 2024/7/11.
//

import UIKit

class FilterCriteriaVideoInfoViewController: QIBaseViewController {
    
    var previewImg = UIImage()
    var originalImg = UIImage()
    /// 原图在本地服务器地址
    var originalImgUrl = ""
    /// 原图地址
    var raw_file_path = ""
    /// 时间名称
    var event_name = ""
    /// 推荐列表数据
    var model = QIBeautyRecommenListModel()
    /// 妆容详情页面传递
    var Local_Save_Path = ""
    /// 五官检测结果
    var wuGuanModel = QIFiveSensesResultModel()
    var videoCode = ""
    /// 收藏ID
    var followID = ""
    /// 妆容json
    var makeupDict = [String: String]()
    private var videoModelArr = [QIBeautyMakeupVideoListModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "美妆推荐"

        addUI()
        getData()
    }
    
    
    private func addUI() {
        showGradientColor()
        
        rightBtn.setImage(UIImage(named: "更多三点"), for: .normal)
        rightBtn.addTarget(self, action: #selector(moreBtnClick), for: .touchUpInside)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBtn)
        
        let scrollView = UIScrollView()
        scrollView.frame = CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - NAV_HEIGHT)
        scrollView.contentSize = CGSize(width: SCREEN_WIDTH, height: scrollView.height + 185)
        view.addSubview(scrollView)
        
        let headerV = VideoInfoHeaderView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 280))
        headerV.backgroundColor = .clear
        headerV.followID = followID
        headerV.previewImg = previewImg
        headerV.model = model
        headerV.event_name = event_name
        headerV.Local_Save_Path = Local_Save_Path
        headerV.originalImg = originalImg
        headerV.wuGuanModel = wuGuanModel
        headerV.raw_file_path = raw_file_path
        headerV.originalImgUrl = originalImgUrl
        headerV.makeupDict = makeupDict
        scrollView.addSubview(headerV)
        
//        scrollView.addSubview(tableView)
        
        let tableScrollView = UIScrollView()
        tableScrollView.frame = CGRect(x: 0, y: 280, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - NAV_HEIGHT)
        tableScrollView.contentSize = CGSize(width: 500, height: scrollView.height + 220)
        scrollView.addSubview(tableScrollView)
//        
//        let headerView = VideoInfoTableHeaderView(frame: CGRect(x: 0, y: 0, width: 500, height: 40))
//        tableScrollView.addSubview(headerView)
        
        tableView.frame = CGRect(x: 0, y: 0, width: 500, height: tableScrollView.height)
        tableScrollView.addSubview(tableView)
    }
    
    private func getData() {
        QINetworkManager.requestJson(urlStr: "/Skin/get_cosmetics", parameters: ["video_id": model.vModel.id]) { json in
            print(json)
            self.videoModelArr = json.arrayObject?.kj.modelArray(QIBeautyMakeupVideoListModel.self) ?? [QIBeautyMakeupVideoListModel]()
            self.tableView.reloadData()
        }
    }
    
    @objc private func moreBtnClick() {
        let alertCtl = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        alertCtl.addAction(UIAlertAction(title: "分享", style: .default, handler: { (_) in
            
        }))
        alertCtl.addAction(UIAlertAction(title: "保存至本地", style: .default, handler: { (_) in
            
        }))
        alertCtl.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (_) in
            
        }))
        self.present(alertCtl, animated: true)
    }
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: CGRect.zero, style: .plain)
        tableView.frame = CGRect(x: 0, y: 280, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - NAV_HEIGHT)
        tableView.contentSize = CGSize(width: 500, height: SCREEN_HEIGHT)
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        } else {}
        return tableView
    }()

}



extension FilterCriteriaVideoInfoViewController: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return videoModelArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        40
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = VideoInfoTableHeaderView(frame: CGRect(x: 0, y: 0, width: 500, height: 40))
        return headerView
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = FilterCriteriaVideoInfoViewCell()
        cell.selectionStyle = .none
        cell.model = videoModelArr[indexPath.row]
        return cell
    }
    
}



class FilterCriteriaVideoInfoViewCell: UITableViewCell {
    
    private var timeL = UILabel()
    private var typeL = UILabel()
    private var brandL = UILabel()
    private var ProductNameL = UILabel()
    private var colorL = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addUI()
    }
    func addUI() {
        
        let bgView = UIView()
        bgView.frame = CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: self.height)
        bgView.backgroundColor = .white
        self.addSubview(bgView)
        
        let leftImgV = UIImageView().then {
            $0.frame = CGRect(x: 15, y: 10, width: 22, height: 22)
            $0.image = UIImage(named: "推薦搜索")
            $0.contentMode = .scaleAspectFill
        }
        self.addSubview(leftImgV)
        
        timeL = UILabel().then({
            $0.frame = CGRect(x: leftImgV.right + 25, y: 0, width: 70, height: self.height)
            $0.text = "00:00:24-00:00:34"
            $0.textColor = Color333333
            $0.numberOfLines = 2
            $0.font = FONT(ofSize: 12)
        })
        self.addSubview(timeL)
        
        typeL = UILabel().then({
            $0.frame = CGRect(x: timeL.right + 10, y: 0, width: 50, height: self.height)
            $0.text = "底妆"
            $0.textColor = Color333333
            $0.font = FONT(ofSize: 12)
        })
        self.addSubview(typeL)
        
        brandL = UILabel().then({
            $0.frame = CGRect(x: typeL.right, y: 0, width: 72, height: self.height)
            $0.text = "MAOGEPING"
            $0.textColor = Color333333
            $0.font = FONT(ofSize: 12)
        })
        self.addSubview(brandL)
        
        ProductNameL = UILabel().then({
            $0.frame = CGRect(x: brandL.right + 5, y: 0, width: 100, height: self.height)
            $0.text = "无暇双色遮瑕膏"
            $0.textColor = Color333333
            $0.font = FONT(ofSize: 12)
        })
        self.addSubview(ProductNameL)
        
        colorL = UILabel().then({
            $0.frame = CGRect(x: ProductNameL.right + 10, y: 0, width: 100, height: self.height)
            $0.text = "红色"
            $0.textColor = Color333333
            $0.font = FONT(ofSize: 12)
        })
        self.addSubview(colorL)
    }
    
    var model = QIBeautyMakeupVideoListModel() {
        didSet {
            timeL.text = model.appearance_time
            typeL.text = model.type
            brandL.text = model.brand
            ProductNameL.text = model.product_name
            colorL.text = model.color_code
            
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

